/*
 * Copyright (c) 2010 Mathias Garbe <code@mathias-garbe.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "client.hpp"
#include "AddPathWindow.hpp"

AddPathWindow::AddPathWindow(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags)
{
    QWidget::setWindowModality(Qt::ApplicationModal);

    ui.setupUi(this);

    connect(this->ui.pushAdd, SIGNAL(clicked()), this, SLOT(pushAdd()));
    connect(this->ui.pushBrowse, SIGNAL(clicked()), this, SLOT(setFile()));
}

AddPathWindow::~AddPathWindow()
{

}

AdvancedGameInfo AddPathWindow::show(AdvancedGameInfo gameInfo)
{
    // Move window to middle of the screen
    QRect screen = QApplication::desktop()->screenGeometry();
    this->move(screen.width()/2 - this->width()/2, screen.height()/2 - this->height()/2);
    QWidget::show();

    this->ui.lineName->setText(gameInfo.getDescription().c_str());
    this->ui.linePath->setText(gameInfo.getExe().c_str());
    this->ui.lineArguments->setText(gameInfo.getArgs().c_str());

    this->closed = false;
    this->saveData = false;

    while(!this->closed)
    {
        Qt::QAppInstance()->processEvents(QEventLoop::AllEvents, 1);
        Qt::QAppInstance()->flush();
    }

    this->hide();

    AdvancedGameInfo newGameInfo(Games::NOGAME);
    if(saveData)
    {
        newGameInfo = AdvancedGameInfo(Games::NOGAME, this->ui.linePath->text().toStdString(), this->ui.lineArguments->text().toStdString(), this->ui.lineName->text().toStdString());
    }
    else
    {
        newGameInfo = gameInfo;
    }

    return newGameInfo;
}

void AddPathWindow::closeEvent(QCloseEvent *event)
{
    this->closed = true;
}
